package com.xx.mind;

import com.xx.dto.SubjectBranch;
import org.eclipse.swt.widgets.Display;
import org.xmind.core.*;
import org.xmind.core.style.IStyle;
import org.xmind.core.style.IStyleSheet;
import org.xmind.ui.mindmap.MindMapImageExporter;
import org.xmind.ui.style.Styles;

import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.xmind.core.*;

/**
 * 生成文件: https://blog.csdn.net/yzw2943295968/article/details/130510241
 * 解析文件：https://www.cnblogs.com/flower-dance/p/16616608.html
 */
public class FishXmind {

    public static void main(String[] args) {
        xmindExport();
    }

    /**
     * 导出Xmind文件
     */
    public static void xmindExport() {
        // 创建Xmind工作簿
        IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder();
        IWorkbook workbook = workbookBuilder.createWorkbook();
        ISheet primarySheet = workbook.getPrimarySheet();
        ITopic topTopic = primarySheet.getRootTopic();

        topTopic.setTitleText("Xmind中心主题");
//        topTopic.setStructureClass("org.xmind.ui.fishbone.leftHeaded");

        // 设置中心主题样式
        IStyleSheet styleSheet = workbook.getStyleSheet();
        IStyle style = styleSheet.findStyle(topTopic.getStyleId());
        if (style == null) {
            style = styleSheet.createStyle(IStyle.TOPIC);
            styleSheet.addStyle(style, IStyleSheet.NORMAL_STYLES);
        }
        style.setProperty(Styles.FillColor, "#00BC7B");
        style.setProperty(Styles.TextColor, "#FFFFFF");
        style.setProperty(Styles.FontWeight, Styles.FontWeight);
        style.setProperty(Styles.FontSize, "20pt");
        style.setProperty(Styles.LineWidth, "2pt");
        style.setProperty(Styles.LineClass, Styles.BRANCH_CONN_STRAIGHT);
        style.setProperty(Styles.LineColor, Styles.NONE);
        topTopic.setStyleId(style.getId());

        // 添加备注内容
        IPlainNotesContent plainContent = (IPlainNotesContent) workbook.createNotesContent(INotes.PLAIN);
        plainContent.setTextContent("这是备注内容!");
        INotes notes = topTopic.getNotes();
        notes.setContent(INotes.PLAIN, plainContent);

        // 添加五个测试主题分支，可以根据业务需求动态增加或减少数据
        for (int i = 0; i < 2; i++) {
            Random random = new Random();
            // 随机主题背景颜色
            String colorCode = "#E6E6FA";//XmindConstant.matterColorArray[random.nextInt(6)];
            // 准备分支主题子内容
            List<SubjectBranch> branchList = new ArrayList<>();
            for (int k = 0; k < 7; k++) {
                branchList.add(SubjectBranch.builder().branchContent(i + "号主题分支子内容" + k).build());
            }
            buildTopic(workbook, topTopic, "主题分支" + i + "号", colorCode, branchList);
        }

        try {
            workbook.save(Files.newOutputStream(Paths.get("./fish.xmind")));
        } catch (Exception e) {
            System.out.println("Xmind思维导图生成失败!");
        }


        try {
            // 创建导出器
            MindMapImageExporter exporter = new MindMapImageExporter(new Display());

            // 设置导出选项
            exporter.setTargetWorkbook(workbook);
            exporter.setTargetStream(new FileOutputStream("d:/a.png"));

            // 执行导出
            exporter.export();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 构建主题
     * <p>
     * 结构类型	结构类名
     * 地图（默认）	空字符串
     * 左头鱼骨	org.xmind.ui.fishbone.leftHeaded
     * 右头鱼骨	org.xmind.ui.fishbone.rightHeaded
     * 左逻辑图	org.xmind.ui.logic.left
     * 右逻辑图	org.xmind.ui.logic.right
     * 向下组织结构图	org.xmind.ui.org-chart.down
     * 向上组织结构图	org.xmind.ui.org-chart.up
     * 电子表格	org.xmind.ui.spreadsheet
     * 左树	org.xmind.ui.tree.left
     * 正确的树	org.xmind.ui.tree.right
     * 漂浮的	org.xmind.ui.map.floating
     *
     * @param workbook   工作簿
     * @param topTopic   顶级主题
     * @param itemName   主题名称
     * @param colorCode  主题背景颜色
     * @param branchList 分支列表
     */
    private static void buildTopic(IWorkbook workbook, ITopic topTopic, String itemName, String colorCode, List<SubjectBranch> branchList) {
        ITopic rootTopic = workbook.createTopic();
//        rootTopic.setStructureClass(XmindConstant.leftFish);
        rootTopic.setTitleText(itemName);
        IStyleSheet styleSheet = workbook.getStyleSheet();
        IStyle style = styleSheet.findStyle(rootTopic.getStyleId());
        if (style == null) {
            style = styleSheet.createStyle(IStyle.TOPIC);
            styleSheet.addStyle(style, IStyleSheet.NORMAL_STYLES);
        }
        Random random = new Random();
//        style.setProperty(Styles.FillColor, XmindConstant.superviseColorArray[random.nextInt(9)]);
        style.setProperty(Styles.TextColor, "#FFFFFF");
        style.setProperty(Styles.FontWeight, Styles.FontWeight);
        style.setProperty(Styles.FontSize, "20pt");
        style.setProperty(Styles.LineWidth, "2pt");
        style.setProperty(Styles.LineColor, Styles.DEF_TOPIC_LINE_COLOR);
        rootTopic.setStyleId(style.getId());

        for (SubjectBranch subjectBranch : branchList) {
            ITopic topicSon = workbook.createTopic();
            topicSon.setTitleText(subjectBranch.getBranchContent());
            IStyleSheet sonStyleSheet = workbook.getStyleSheet();
            IStyle sonStyle = sonStyleSheet.findStyle(topicSon.getStyleId());
            if (sonStyle == null) {
                sonStyle = styleSheet.createStyle(IStyle.TOPIC);
                styleSheet.addStyle(sonStyle, IStyleSheet.NORMAL_STYLES);
            }
            sonStyle.setProperty(Styles.FillColor, colorCode);
            sonStyle.setProperty(Styles.ShapeClass, Styles.TOPIC_SHAPE_ROUNDEDRECT);
            sonStyle.setProperty(Styles.TextColor, "#000229");
            sonStyle.setProperty(Styles.FontWeight, Styles.FontWeight);
            sonStyle.setProperty(Styles.FontSize, "16pt");
            sonStyle.setProperty(Styles.LineWidth, "2pt");
            sonStyle.setProperty(Styles.LineColor, Styles.DEF_TOPIC_LINE_COLOR);
            topicSon.setStyleId(sonStyle.getId());

            // 增加鱼骨图分支
            for (int i = 0; i < 2; i++) {
                ITopic topicItem = workbook.createTopic();
                topicItem.setTitleText("分支" + i);
                IStyleSheet itemStyleSheet = workbook.getStyleSheet();
                IStyle itemStyle = itemStyleSheet.findStyle(topicItem.getStyleId());
                if (itemStyle == null) {
                    itemStyle = styleSheet.createStyle(IStyle.TOPIC);
                    styleSheet.addStyle(itemStyle, IStyleSheet.NORMAL_STYLES);
                }
                itemStyle.setProperty(Styles.TextColor, "#000229");
                itemStyle.setProperty(Styles.ShapeClass, Styles.TOPIC_SHAPE_ROUNDEDRECT);
                itemStyle.setProperty(Styles.FontWeight, Styles.FontWeight);
                itemStyle.setProperty(Styles.FontSize, "14pt");
                itemStyle.setProperty(Styles.LineWidth, "2pt");
                itemStyle.setProperty(Styles.LineColor, Styles.DEF_TOPIC_LINE_COLOR);
                topicItem.setStyleId(itemStyle.getId());
                topicSon.add(topicItem);
            }
            rootTopic.add(topicSon);
        }
        topTopic.add(rootTopic);
    }
}
